<?php
	$_nav="service";
	$_title='服务';
	include_once 'head.php';
	$servicec=new service();
	$userc=new user();
	$user_collection='user_collection';		//会员收藏表
	$zs_service_praise='zs_service_praise';	//服务获赞表
	$service_id=$_GET['id'];				//服务id

	$ip = $_SERVER['REMOTE_ADDR'];			//当前ip

	//点击量+1
	$servicec->serviceClickAdd('id="'.$_GET['id'].'"');

	//服务内容
	$where=' and s.id="'.$service_id.'"';
	$key='s.*';
	$service=$servicec->serviceInfo($where,$key);

	//是否收藏
	$sql=$user_collection.' uc where user_id="'.$user_id.'" and type="service" 
		 and type_id="'.$service_id.'"';
	$is_collect=$commonc->getInfo($sql);
	$service['is_collect']=$is_collect;

	//添加、取消收藏
	if($_GET['action']=='collect'){
		$isId=$_POST['isId'];
		if($user_id>0){
			if($isId>0){
				//取消收藏
				$res=$commonc->del($user_collection,$isId.',');
				$msg='取消收藏！';
			}else{
				$arr=array('user_id'=>$user_id,'type'=>'service','create_time'=>time(),'type_id'=>$_GET['id']);
				$res=$commonc->insert($user_collection,$arr);
				$id=$commonc->insert_id();
				$msg='收藏成功！';
			}
		}else{
			$res=2;		//未登录
			$msg="您还未登录，请先登录！";
		}
		$result=array('error'=>$res,'msg'=>$msg,'isId'=>$id);
		echo json_encode($result);
		exit();
	}

	//添加、取消赞，一般，烂
	if($_GET['action']=="threePraise"){
		$type=array('bad'=>'烂','just'=>'一般','praise'=>'赞');
		$types=$_POST['types'];
		$isId=$_POST['isId'];
		if($user_id>0){
			if($isId>0){
				//取消
				$res=$commonc->del($zs_service_praise,$isId.',');
				$msg=$type[$types].'已取消！';
				$html=$type[$types];
			}else{
				$arr=array('user_id'=>$user_id,'type'=>$types,'create_time'=>time(),'service_id'=>$_GET['id']);
				$res=$commonc->insert($zs_service_praise,$arr);
				$id=$commonc->insert_id();
				$msg=$type[$types].'成功！';
				$html='取消'.$type[$types];
			}
		}else{
			$res=2;		//未登录
			$msg="您还未登录，请先登录！";
		}
		$result=array('error'=>$res,'msg'=>$msg,'html'=>$html,'isId'=>$id);
		echo json_encode($result);
		exit();
	}

	//是否赞过
	$sql=$zs_service_praise.' where user_id="'.$user_id.'" and service_id="'.$_GET['id'].'"';
	$isPraise=$commonc->getList($sql);
	if(is_array($isPraise['result'])){
		foreach($isPraise['result'] as $k=>$v){
			if($v['type']=='praise'){
				$service['threePraise']['praise']=$v['id'];		
			}
			if($v['type']=='just'){
				$service['threePraise']['just']=$v['id'];	
			}
			if($v['type']=='bad'){
				$service['threePraise']['bad']=$v['id'];
			}
		}
	}
	$service['threePraise']['justName']=$service['threePraise']['just']==''?'一般':'取消一般';
	$service['threePraise']['praiseName']=$service['threePraise']['praise']==''?'赞':'取消赞';
	$service['threePraise']['badName']=$service['threePraise']['bad']==''?'烂':'取消烂';
	
	//服务一级分类
	$where=' AND upid=0';
	$serviceClass=$servicec->serviceClassList($where);
	$smarty->assign('serviceClass',$serviceClass['result']);
	
	if($service!=''){
		//二级分类名称
		$where="  AND id='".$service['class_id']."'";
		$className2=$servicec->serviceClassList($where);
		//一级分类名称
		$where="  AND id='".$className2['result']['0']['upid']."'";
		$className1=$servicec->serviceClassList($where);

		$service['className2']=$className2['result']['0']['class_name'];
		$service['className1']=$className1['result']['0']['class_name'];
		$service['class1_id']=$className1['result']['0']['id'];
	}
	
	$smarty->assign('service',$service);

	//服务相册
	$where=' and sa.service_id="'.$service['id'].'" ORDER BY sa.sort asc';
	$key='sa.*';
	$num=5;
	$albums=$servicec->serviceAlbumList($where,$key,$num);
	$smarty->assign('albums',$albums['result']);


	//用户评论  
	$where=' and a.service_id="'.$service['id'].'"';
	$key="a.*,u.account,u.avatar,u.id as u_id,u1.account as account1,u1.avatar as avatar1,u1.id as u_id1";
	$comment=$servicec->serviceComment($where,$key);
	$comment=$comment['result'];
	for ($i=0; $i <count($comment) ; $i++) { 
		@$comment[$i]['create_time']=date('Y-m-d H:i:s',$comment[$i]['create_time']);
	}

	$smarty->assign('comment',$comment);
	//评论数
	$commentNum=count($comment);
	$smarty->assign('commentNum',$commentNum);


	//评论
	if($_GET['action']=='comment'){
		$table='zs_service_comment';
		$reply_id=$_POST['reply_id']; //回复ID
		$comment=$_POST['comment'];	  //评论内容
		$service_id=$_POST['service_id'];//服务ID

		if($user_id!=''){
			$array=array('service_id'=>$service_id,'user_id'=>$user_id,'comment'=>$comment,'reply_id'=>$reply_id,'create_time'=>time());

			//判断是否自己回复自己(暂时关闭)
			// if($reply_id==$user_id){
			// 	$array['reply_id']=0;
			// }

			$res=$commonc->insert($table,$array);
			if($res){
				$mes='评论成功';
				$time=date('Y-m-d H:i:s',$array['create_time']);
				$user=$userc->userInfo($user_id);
				$userName=$user['account'];
				$userImg=$user['avatar'];

				/*回复AJAX-start*/
				if($array['reply_id']>0){
					$reply=$userc->userInfo($reply_id);
					$replyName=$reply['account'];
					$reply="回复&nbsp;@<a href=user_main.php?type=user&id=".$reply_id.">".$replyName."</a>：";
				}	

				//返回数据
				$html='<div class="pinglun"> <img src="upload/user/small/'.$userImg.'" width="45" height="45" style="display:block; float:left;" />
			        <div class="pinglun_r1">
			          <div class="pinglun_r_left"> <span><b> <a href="user_main.php?type=user&id='.$user_id.'">'.$userName.'</a>：'.$reply.'</b><span class="neirong">'.$comment.'</span></span>
			            <p>'.$time.'</p>
			          </div>
			          <ul class="ul_pinglun_r">
			            <li><a href="#F1" class="a_huifu1" user_id='.$user_id.' name='.$userName.'>回复</a></li>
			          </ul>
			        </div>
			    </div>';
			    /*回复AJAX-end*/
			}else{
				$mes='评论失败，请重试';
			}

			$error=1;
		}else{
			$error=0;
		}
		$arr=array('error'=>$error,'mes'=>$mes,'html'=>$html);
		echo json_encode($arr);
		exit();
	}


	$smarty->display("service_info.html");
?>